package com.example.administrator.androidros.utils;

import android.content.Context;
import android.content.SharedPreferences;

import com.alibaba.fastjson.JSON;
import com.example.administrator.androidros.model.MemberModel;

/**
 * Created by huhaichao on 2018/3/26.
 */

public class AgentSharedPreferences {

    /**
     * 保存用户信息
     */
    public static void saveMemberInfo(Context mContext, MemberModel memberModel) {
        //获取本地私有文件"MEMBER_INFO"
        SharedPreferences preferences = mContext.getSharedPreferences("MEMBER_INFO", Context.MODE_PRIVATE);
        //保存为JSON串
        String json = JSON.toJSONString(memberModel);
        // 写入到私有文件，命名为"MEMBER_MODEL"
        preferences.edit().putString("MEMBER_MODEL", json).apply();
    }

    /**
     * 获取用户信息
     */
    public static MemberModel getMemberInfo(Context mContext) {
        SharedPreferences preferences = mContext.getSharedPreferences("MEMBER_INFO", Context.MODE_PRIVATE);
        String json = preferences.getString("MEMBER_MODEL", null);
        if (json == null) {
            return null;
        }
        return JSON.parseObject(json, MemberModel.class);
    }

    /**
     * 上一次登录id
     */
    public static void setLastLoginId(Context context, String id) {
        SharedPreferences preferences = context.getSharedPreferences(
                "LAST_LOGIN_INFO", Context.MODE_PRIVATE);
        preferences.edit().putString("LAST_LOGIN_ID", id).apply();
    }

    public static String getLastLoginId(Context mContext) {
        SharedPreferences preferences = mContext.getSharedPreferences("LAST_LOGIN_INFO", Context.MODE_PRIVATE);
        String json = preferences.getString("LAST_LOGIN_ID", "");
        return json;
    }

}
